﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectSemIII.Process;
using ProjectSemIII.Entity;

namespace ProjectSemIII
{
    public partial class Register : System.Web.UI.Page
    {
        private UserProcess process;
        private Generator gene;
        protected void Page_Load(object sender, EventArgs e)
        {
            process = new UserProcess();
            gene = new Generator();
            if (IsPostBack)
            {
                if (!(String.IsNullOrEmpty(txtPassword.Text.Trim())))
                {
                    txtPassword.Attributes["value"] = txtPassword.Text;
                    txtReEnterPassword.Attributes["value"] = txtReEnterPassword.Text;
                }
            }
        }

        protected void btRegister_Click(object sender, EventArgs e)
        {
            if (this.checkValidateInfo())
            {
                string salt = gene.GetUniqueKey(19);
                User u = new User();
                u.Username = txtUserName.Text;
                u.Salt = salt;
                string md5Password = gene.CreateMD5Hash(txtPassword.Text);
                string passwordMd5 = md5Password + salt;
                u.Password = gene.CreateMD5Hash(passwordMd5);
                u.RollNo = txtRollNo.Text;
                u.FullName = txtFullName.Text;
                u.Class = txtClass.Text;
                u.Specification = txtSpecification.Text;
                u.Section = txtSection.Text;
                u.JoinDate = joinDate.SelectedDate;
                u.Actived = 0;
                if (student.Checked == true)
                {
                    u.Role = 1;
                }
                else
                {
                    u.Role = 2;
                }
                int result = process.createNewUser(u);
                if (result == 10)
                {
                    divCenter.Visible = false;
                    lbStatus.Text = "Register Sucessfull! Now you can login to system!";
                    registerStatus.Visible = true;
                }
                else
                {
                    divCenter.Visible = false;
                    errorSpan.InnerHtml = "An error in create new user. Please <a id=\"ucLogin_hlRegister\" href=\"Register.aspx\" style=\"color:white;\">Click Here</a> to try agian!";
                    registerStatus.Visible = true;
                }
            }
        }

        private bool checkValidateInfo()
        {
            bool flag = true;
            if (process.checkUserNameExits(txtUserName.Text))
            {
                flag = false;
                error.Text = "This username already exists!";
                errorDiv.Visible = true;
            }
            else
            {
                if (process.checkRollNoExits(txtRollNo.Text))
                {
                    flag = false;
                    error.Text = "This Roll No already exists!";
                    errorDiv.Visible = true;
                }
                else
                {
                    if (joinDate.SelectedDate.ToString() == "01/01/0001 12:00:00 SA")
                    {
                        flag = false;
                        error.Text = "Please select join date!";
                        errorDiv.Visible = true;
                    }
                }
            }
            return flag;
        }

        protected void brClear_Click(object sender, EventArgs e)
        {

        }
    }
}